package outoforder.leetcode01;

import org.junit.Test;

import java.util.ArrayDeque;

/**
 * @author shhjiang_001
 * @create 2020-07-10 17:24
 */

// 每日气温
public class Solution739 {
    public int[] dailyTemperatures(int[] T) {
        int length = T.length;
        int[] ints = new int[length];
        ArrayDeque<Integer> deque = new ArrayDeque<>();
        // 逆序遍历
        for (int i = length-1; i >= 0; i--) {
            // 如果栈顶索引的元素小于i对应索引的元素，则对栈pop()操作
            while (!deque.isEmpty() && T[deque.peek()] <= T[i]){
                deque.pop();
            }
            ints[i] = deque.isEmpty()? 0:deque.peek()-i;
            deque.push(i);
        }
        return ints;
    }
    @Test
    public void test(){
        dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73});
    }
}
